import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import VuexStore from '@/store/index.js'  //导入Vuex 获取token
// 重写VueRouter的push函数.
const OriginPush=VueRouter.prototype.push;
VueRouter.prototype.push=function(location,onResolve,onReject){
  if(onResolve||onReject) return OriginPush.call(this,location,onResolve,onReject)
  // 如果出现异常提示，不显示异常
  return OriginPush.call(this,location).catch(err=>err);
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',   //首页
    name: 'Home',
    component: Home,
  },
  {
    path: '/category', //分类
    name: 'Category',
    component: () => import('@/views/Category.vue')
  },
  {
    path:'/cart',   //购物车
    name:'Cart',
    meta:{
      needLogin:true,
    },
    component: () => import('@/views/Cart.vue')
  },
  {
    path:'/user',   //个人中心
    name:'User',
    meta:{
      needLogin:true,
    },  //做登录标记
    component: () => import('@/views/User.vue')
  },
  {
    path:"/search",   //搜索
    name:'Search',
    component:() => import('@/views/Search.vue')
  },
  {
    path:'/login',   //登录
    name:'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path:'/register',   //注册
    name:'Register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/productlist',  //产品列表
    name: 'ProductList',
    props: true,
    component: () => import('@/views/ProductList.vue')
  },
  {
    path: '/detail',  //商品详情
    name: 'Detail',
    props: true,
    meta: {//路由元信息 
      hideNav: true,
    },
    component: () => import('@/views/Detail.vue')
  },
  
  {
    path: '/moreShop/:num',  //更多商品
    name: 'MoreShop',
    props: true,
    component: () => import('@/views/MoreShop.vue')
  },

  {
<<<<<<< HEAD
    path: '/checkoutsuccess/:sid',  //支付成功信息
=======
    path: '/checkoutsuccess/:sid',
>>>>>>> db4a5f094eee38ffd715af53d411876e5007d1b9
    name: 'CheckoutSuccess',
    props: true,
    meta: {
      hideNav: true,
    },
    component: () => import('@/views/CheckoutSuccess.vue')
  },
  {
<<<<<<< HEAD
    path: '/order/:sid',  //个人中心-我的订单-订单信息-每件订单详情信息
=======
    path: '/order/:sid',
>>>>>>> db4a5f094eee38ffd715af53d411876e5007d1b9
    name: 'Order',
    props: true,
    component: () => import('../views/Order.vue')
  },
  {
<<<<<<< HEAD
    path: '/myOrder/:id',  //个人中心-我的订单-订单信息
=======
    path: '/myOrder/:id',  //个人中心
>>>>>>> db4a5f094eee38ffd715af53d411876e5007d1b9
    name: 'MyOrder',
    props: true,
    component: () => import('@/views/MyOrder.vue')
  },
  {
<<<<<<< HEAD
    path: '/submit/:sid', //购物车-提交订单 的信息
=======
    path: '/submit/:sid', //提交订单
>>>>>>> db4a5f094eee38ffd715af53d411876e5007d1b9
    name: 'SubMit',
    props: true,
    meta: {
      hideNav: true,
    },
    component: () => import('@/views/SubMit.vue')
  },
  

]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
});
// 全局路由守卫
router.beforeEach((to,from,next)=>{
  // 拿取路由里面的meta信息判断当前进入的路由是否需要登录才能进去
  if(to.meta.needLogin && !VuexStore.getters.logintoken){
<<<<<<< HEAD
    next({path:'/login'})
=======
    return next({path:'/login'})
>>>>>>> db4a5f094eee38ffd715af53d411876e5007d1b9
  }
  next();
});

export default router
